// Copyright (c) 2024, the WwMusic project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

package com.springfairy.util;

import com.springfairy.dao.UserMapper;
import com.springfairy.model.User;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class UserUtils {
    @Autowired
    private static UserMapper userMapper;

    public static User getLoginUser(HttpServletRequest request) {
        Integer userId = getLoginUserId(request);
        if(userId == null) {
            return null;
        }
        return userMapper.selectById(userId);
    }

    public static Integer getLoginUserId(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        Integer userId = null;
        try {
            String userIdString = session.getAttribute("userId").toString();
            userId = Integer.parseInt(userIdString);
        } catch (Exception e) {
            return null;
        }
        return userId;
    }
}
